package q1_50.q1_10;

public class Solution4 {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int[] nums3 = new int[nums1.length + nums2.length];
        int i = 0, j = 0, k = 0;
        //合并两个有序数组
        while (i < nums1.length && j < nums2.length) {
            if (nums1[i] < nums2[j]) {
                nums3[k++] = nums1[i++];
            } else {
                nums3[k++] = nums2[j++];
            }
        }
        while (i < nums1.length) {
            nums3[k++] = nums1[i++];
        }
        while (j < nums2.length) {
            nums3[k++] = nums2[j++];
        }
        if ((nums3.length & 1) == 0)
            return (nums3[nums3.length / 2] + nums3[nums3.length / 2 - 1]) / 2.0;
        else return nums3[nums3.length / 2];
    }

    public static void main(String[] args) {
        Solution4 solution4 = new Solution4();
        int[] nums1 = {1, 3};
        int[] nums2 = {2, 7};
        System.out.println(solution4.findMedianSortedArrays(nums1, nums2));
    }
}
